<?php

/**
 * Created by IntelliJ IDEA.
 * User: XingHuo
 * Date: 15/4/7
 * Time: 下午10:24
 */
class AdminMongo
{
    function index()
    {
        $servers = Boot::config('servers');
        $servers = CommonHelper::formatServerList($servers, false);
        Boot::view('/admin/index.php', array(
            'servers' => $servers
        ));
    }

    function dbs()
    {
        $node = Boot::request('node', '');

        $tree = array();
        $servers = Boot::config('servers');

        $servers = CommonHelper::formatServerList($servers, $onlyId = true);

        if (!in_array($node, $servers)) {
            $data = CommonHelper::parseNodeName($node);
            list($server, $db) = $data;
            $mg = Boot::getMongo($server);

            $db = $mg->selectDB($db);
            $collections = $db->listCollections();

            foreach ($collections as $collection) {
//                MongoCollection  $collection;
                $colName = CommonHelper::getCollectionName($collection);

                $tree[] = array(
                    'text' => '' . $collection->getName(),
                    'id' => $node . '' . $colName,
                    'leaf' => true,
                    'cls' => 'nodeicon'
                );
            }

        } else {
            $nodeInfo = CommonHelper::parseNodeName($node);
            $mg = Boot::getMongo($nodeInfo['0']);

            $offset = Boot::request('offset', '0');
            $limit = Boot::request('limit', '10');
            $server = $node;
            $dbs = $mg->listDBs();
            foreach ($dbs['databases'] as $db) {

                $tree[] = array(
                    'text' => $db['name'],
                    'id' => $server . CommonHelper::getDbName($db),
                    'leaf' => false,
                    'cls' => 'nodeicon'
                );
            }
        }
        Boot::output($tree);
    }

    function collection()
    {
//        $mg = Boot::getMongo();
        $collection = Boot::request('collect', '');
        $nodeInfo = CommonHelper::parseNodeName($collection);

        Boot::view('/admin/collection.php', array(
            'collection' => $collection,
            'colName' => Boot::get($nodeInfo, '2', ''),
            'colId' => 'colId' . md5($collection)
        ));
    }

    function edit()
    {
//        $json_decode = json_decode('{"name":"xinghuo"}');

        $doc = Boot::request('doc', '');
        $collection = Boot::request('col', '');
        list($server, $db, $col) = CommonHelper::parseNodeName($collection);
        $mg = Boot::getMongo($server);

        if ($doc) {
            $action = Boot::request('action', '');
            $dbCol = $mg->$db->$col;
            $json_decode = json_decode($doc);
            if (!$json_decode) {
                $json_error = json_last_error_msg();
                Boot::output(array('msg' => $json_error));
            } elseif ($action == 'add') {
                list($db, $col) = explode('.', Boot::request('col'));
                $dbCol = $mg->$db->$col;
                $json_decode = json_decode($doc);
                if (!$json_decode) {
                    $json_error = json_last_error_msg();
                    Boot::output(array('msg' => $json_error));

                } else {
                    $result = $dbCol->insert($json_decode);
                    if ($result['ok'] == 1) {
                        Boot::output(array('msg' => "保存成功!"));
                    } else {
                        Boot::output(array('msg' => "保存失败!"));
                    }
                }
            } elseif ($action == 'delete') {
                $result = $dbCol->remove($json_decode);
                if ($result['ok'] == 1) {
                    Boot::output(array('msg' => "删除成功!"));
                } else {
                    Boot::output(array('msg' => "保存失败!"));
                }
            }

        } else {
            Boot::output(array('msg' => '添加失败!'));
        }
    }

    function show_collect()
    {
        $tree = array();
        $skip = Boot::request('start', '0');
        $limit = Boot::request('limit', '15');
        $doc = Boot::request('doc', '');
        $where = array();
        if ($doc) {
            $docData = json_decode($doc);
            if ($docData) {
                $where = $docData;
                //$json_error = json_last_error_msg();
            }
        }
        $collection = Boot::request('collect', '');
        list($server, $db, $collection) = $dbinfo = CommonHelper::parseNodeName($collection);
        $mg = Boot::getMongo($server);
        if ($collection) {
            $datas = $mg->$db->$collection->find($where)->skip($skip)->limit($limit);

            $tree = array();
            foreach ($datas as $data) {
                $tree[] = array(
                    'id' => Boot::get($data, '$id', ''),
                    'name' => Boot::get($data, 'name', ''),
                    'descn' => json_encode($data),
                    'sex' => Boot::get($data, 'sex', '')
                );
            }
            $totalProperty = $mg->$db->$collection->count($where);
        }
        Boot::output(array(
            'root' => $tree,
            'totalProperty' => $totalProperty
        ));

    }

    function createCollection()
    {
        $collection = Boot::request('collect', '');
        list($server, $db, $collection) = $dbinfo = CommonHelper::parseNodeName($collection);

        $mg = Boot::getMongo($server);

        $result = $mg->$db->createCollection($collection, array());
        Boot::output(array(
            'success' => $result ? true:false,
            'result' => $result,
            'msg' => $result ? '创建成功!' : '创建失败!'

        ));
    }
    function createDb(){
        $nodeid = Boot::request('nodeid', '');
        list($server, $db, $col) = $dbinfo = CommonHelper::parseNodeName($nodeid);

        $mg = Boot::getMongo($server);


        $result = $mg->$db->createCollection('test');

        Boot::output(array(
            'success' => $result ? true:false,
            'result' => $result,
            'msg' => $result ? '创建成功!' : '创建失败!'
        ));
    }
    function dropDb()
    {
        $nodeid = Boot::request('nodeid', '');
        list($server, $db, $col) = $dbinfo = CommonHelper::parseNodeName($nodeid);

        $mg = Boot::getMongo($server);


        $result = $mg->$db->drop();
        $msg = 'ok';

        Boot::output(array(
            'success' => $msg,
            'result' => $result,
            'msg' => $result ? '删除成功!' : '删除失败!'
        ));
    }

    function dropCollection()
    {
        $nodeid = Boot::request('nodeid', '');
        list($server, $db, $col) = $dbinfo = CommonHelper::parseNodeName($nodeid);
        $mg = Boot::getMongo($server);
        $result = $mg->$db->$col->drop();
        Boot::output(array(
            'success' => true,
            'result' => $result,
            'msg' => $result ? '删除成功!' : "删除失败!"
        ));
    }
}